﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using KqAutoDesign.Base;
using KqAutoDesign.Models;

namespace KqAutoDesign.SonViews
{
    public class WorkFlowCtrlVM : NotifyBase
    {
        public WorkFlowCtrlVM()
        {
            SuspendCmd = new SimpleCommand<object>(CanSuspend, SuspendAct);
            StopCmd = new SimpleCommand<object>(CanStop, StopAct);
            ContinueCmd = new SimpleCommand<object>(CanContinue, ContinueAct);
            OpenCmd = new SimpleCommand<object>(CanOpen, OpenAct);
        }
        private void OpenAct(object obj)
        {
            Process.Start("explorer.exe", Task.Path);
        }

        private bool CanOpen(object arg)
        {
            if(Directory.Exists(Task.Path))
                return true;
            return false;
        }

        private void ContinueAct(object obj)
        {
            throw new NotImplementedException();
        }

        private bool CanContinue(object arg)
        {
            if (Task.WorkFlow.WorkFlowState == WorkFlowStateEnum.Suspended)
                return true;
            return false;
        }

        private void StopAct(object obj)
        {
            throw new NotImplementedException();
        }

        private bool CanStop(object arg)
        {
            if (Task.WorkFlow.WorkFlowState == WorkFlowStateEnum.Running|| 
                Task.WorkFlow.WorkFlowState == WorkFlowStateEnum.Suspended)
                return true;
            return false;
        }

        private void SuspendAct(object obj)
        {
            throw new NotImplementedException();
        }

        private bool CanSuspend(object arg)
        {
            if (Task.WorkFlow.WorkFlowState == WorkFlowStateEnum.Running)
                return true;
            return false;
        }

        public ICommand SuspendCmd { get; set; }
        public ICommand StopCmd { get; set; }
        public ICommand ContinueCmd { get; set; }
        public ICommand StartCmd { get; set; }
        public ICommand OpenCmd { get; set; }

        private ModuleBase _selMd;
        public ModuleBase SelectedModule
        {
            get { return _selMd; }
            set
            {
                if (value != _selMd)
                {
                    _selMd = value;
                    OnPropertyChanged(nameof(SelectedModule));
                    if(value != null)
                    {
                        TotalParams = new GroupParams();
                        foreach (var pm in _selMd.BatchScript.ScriptParams)
                        {
                            TotalParams.Add(new GroupScriptParam(pm, _selMd.BatchScript.Name));
                        }
                        foreach (var fs in _selMd.FunctionScripts)
                        {
                            foreach (var pm in fs.ScriptParams)
                            {
                                TotalParams.Add(new GroupScriptParam(pm, fs.Name));
                            }
                        } 
                    }
                }
            }
        }

        private GroupParams _ttpms;
        public GroupParams TotalParams
        {
            get { return _ttpms; }
            set
            {
                if(value != _ttpms)
                {
                    _ttpms = value;
                    OnPropertyChanged(nameof(TotalParams));
                }
            }
        }
        public TaskInfo Task { get; set; }
        public string TopDescription
        {
            get { return Task.Name + "-" + Task.StatuDescription; }
        }
    }
}
